using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Game.RemindModule
{
    [RequireComponent(typeof(XImage))]
    public class RedDotHandler : MonoBehaviour
    {
        public static List<RedDotHandler> redDots = new List<RedDotHandler>();

        public string nodeName;
        public string treeRoot { get; private set; }
        XImage redDotImg;

        public bool disPlay
        {
            get
            {
                //return gameObject.activeSelf;
                return redDotImg == null ? false : redDotImg.enabled;
            }
            set
            {
                //gameObject.SetActive(value);
                if (redDotImg != null)
                    redDotImg.enabled = value;
            }
        }

        public void SetNodeName(string str)
        {
            if (nodeName == str) return;
            nodeName = str;
            RefreshRoot();
            disPlay = RemindManager.IsDirty(nodeName);
        }

        void RefreshRoot()
        {
            if (!string.IsNullOrEmpty(treeRoot) && nodeName.IndexOf(treeRoot) == 0) return;
            var temp = nodeName.IndexOf('/');
            if (temp > 0)
                treeRoot = nodeName.Substring(0, temp);
            else
                treeRoot = nodeName;
        }

        protected void Awake()
        {
            RefreshRoot();
            redDots.Add(this);
            if (gameObject.TryGetComponent<XImage>(out redDotImg))
            {
                redDotImg.raycastTarget = false;
            }
        }


        private void OnEnable()
        {
            if (!string.IsNullOrEmpty(nodeName))
                disPlay = RemindManager.IsDirty(nodeName);
        }

        private void OnDisable()
        {

        }


        void OnDestroy()
        {
            redDots.Remove(this);
        }

    }
}
